package com.ljkj.cordial.chat.notify;

import android.content.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;

import com.lanjiu.lib.utils.log.Logger;

import me.leolin.shortcutbadger.ShortcutBadger;

/**
 * @description:
 * @author: Panwei
 * @date: 2020/2/10 15:17
 */
public class AppBadgerManager {

    private static final String ROM = "MAGNIWILL";

    private AppBadgerManager() {
    }

    public static void count(Context context, int count) {
        if (ROM.equals(Build.MANUFACTURER)) {
            magniwillCount(context, count);
        } else {
            if (ShortcutBadger.isBadgeCounterSupported(context)) {
                ShortcutBadger.applyCount(context, count);
            } else {
                Logger.print("AppBadgerManager=====>" + Build.MANUFACTURER + "不支持Badge");
            }
        }
    }

    public static void clean(Context context) {
        if (ROM.equals(Build.MANUFACTURER)) {
            magniwillCount(context, 0);
        } else {
            if (ShortcutBadger.isBadgeCounterSupported(context)) {
                ShortcutBadger.removeCount(context);
            } else {
                Logger.print("AppBadgerManager=====>" + Build.MANUFACTURER + "不支持Badge");
            }
        }
    }

    private static void magniwillCount(Context context, int count) {
        try {
            Bundle extra = new Bundle();
            extra.putInt("app_badge_count", count);
            extra.putString("app_badge_component_name", new ComponentName("com.ljkj.cordial.chat", "com.ljkj.cordial.chat.activity.SplashActivity").flattenToString());
            extra.putInt("uid", Binder.getCallingUid());

            Uri uri = Uri.parse("content://com.ljt.launcher.unreadbadge");
            Uri.Builder builder = uri.buildUpon();
            builder.encodedAuthority("" + 0 + "@" + uri.getEncodedAuthority());
            context.getContentResolver().call(builder.build(), "setAppUnreadCount", null, extra);
        } catch (Exception e) {
            Logger.print("AppBadgerManager=====>" + Build.MANUFACTURER + "显示Badge异常");
        }
    }
}
